﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.Generic;

namespace AttentenceCheckSystem
{
    /// <summary>
    /// Calender.xaml 的交互逻辑
    /// </summary>
    public partial class Calender : UserControl
    {
        public static DependencyProperty DateProperty;

        static Calender()
        {
            DateProperty = DependencyProperty.Register("Date",
                                                        typeof(DateTime), typeof(Calender),
                                                        new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnDatePropertyChanged)));
        }

        static void OnDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            Calender dp = target as Calender;
            //dp.Date = (DateTime)e.NewValue;
        }

        public Calender()
        {
            InitializeComponent();
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set
            {
                SetValue(DateProperty, value);
                RefreshCanvas();
            }
        }

        void RefreshCanvas()
        {
            DateTime currentDate = common.GetCheckStartDate(Date);
            DateTime endDate = common.GetCheckEndDate(Date);

            int row = 1;
            while (currentDate != endDate)
            {
                CalenderDay dayItem = new CalenderDay();
                if (currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday)
                    dayItem.IsHoliday = true;
                dayItem.Day = currentDate;
                dayItem.SetValue(Grid.ColumnProperty, Convert.ToInt32(currentDate.DayOfWeek));
                dayItem.SetValue(Grid.RowProperty, row);
                Grid1.Children.Add(dayItem);
                if (currentDate.DayOfWeek == DayOfWeek.Saturday)
                    ++row;
                currentDate = currentDate.AddDays(1);
            }
        }

        public void ClearHolidays()
        {
            foreach (ContentControl ctrl in Grid1.Children)
            {
                if (ctrl is CalenderDay)
                {
                    CalenderDay dayItem = ctrl as CalenderDay;
                    dayItem.IsHoliday = false;
                }
            }
        }

        public void SetHoliday(DateTime dt, bool value)
        {
            foreach (ContentControl ctrl in Grid1.Children)
            {
                if (ctrl is CalenderDay)
                {
                    CalenderDay dayItem = ctrl as CalenderDay;
                    if (dayItem.Day == dt)
                    {
                        dayItem.IsHoliday = value;
                        return;
                    }
                }
            }
        }

        public List<DateTime> Holidays()
        {
            List<DateTime> result = new List<DateTime>();
            foreach (ContentControl control in Grid1.Children)
            {
                if (control is CalenderDay)
                {
                    CalenderDay calenderDay = control as CalenderDay;
                    if (calenderDay.IsHoliday)
                        result.Add(calenderDay.Day);
                }
            }
            return result;
        }
    }
}
